/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package clock._11.rodionova;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.GregorianCalendar;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 *
 * @author 10rodionova
 */
public class NewDrawPanel extends JPanel {

    private int hours;
    private int minutes;
    private int seconds;
    private Image bgImage;

    @Override
    protected void paintComponent(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.translate((getWidth() - 250)/2, (getHeight() - 80)/2);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //g2d.setColor(Color.WHITE);
        //g2d.fillRect(2, 2, 170, 60);
        g2d.drawImage(bgImage, 2, 2, 250, 80, Color.CYAN, this);
        g2d.scale(3, 4);
        g2d.setColor(new Color(0x660066));

        int j = hours/10;
        int k = hours%10;

        digit(j, g2d);
        g2d.translate(10, 0);
        digit(k, g2d);

        g2d.translate(10, 0);
        g2d.setColor(Color.BLACK);
        g2d.drawLine(10, 7, 10, 7);
        g2d.drawLine(10, 12, 10, 12);

        g2d.setColor(new Color(0x660066));
        g2d.translate(10, 0);

        int c = minutes/10;
        int d = minutes%10;

        digit(c, g2d);
        g2d.translate(10, 0);
        digit(d, g2d);

        g2d.translate(10, 0);
        g2d.setColor(Color.BLACK);
        g2d.drawLine(10, 7, 10, 7);
        g2d.drawLine(10, 12, 10, 12);

        g2d.setColor(new Color(0x660066));
        g2d.translate(10, 0);

        int a = seconds/10;
        int b = seconds%10;

        digit(a, g2d);
        g2d.translate(10, 0);
        digit(b, g2d);
    }

    public NewDrawPanel() {
        //load image
        try {
            InputStream im = NewDrawPanel.class.getResourceAsStream("0b03e4bbd55bd3b5b4ce14c188763138.jpg");
            bgImage = ImageIO.read(im);
            im.close();
        } catch (IOException ex) {
            System.out.println("file not found");
        }

        //init timer
        Timer t = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GregorianCalendar cc = new GregorianCalendar();
                seconds = cc.get(GregorianCalendar.SECOND);
                minutes = cc.get(GregorianCalendar.MINUTE);
                hours = cc.get(GregorianCalendar.HOUR);
                repaint();
            }
        });
        t.start();
    }

    //-----------------------------------------------------------

    private void digit1(Graphics2D g2d) {
        draw3(g2d);
        draw6(g2d);
    }

    private void digit2(Graphics2D g2d) {
        draw1(g2d);
        draw3(g2d);
        draw4(g2d);
        draw5(g2d);
        draw7(g2d);
    }

    private void digit3(Graphics2D g2d) {
        draw1(g2d);
        draw3(g2d);
        draw4(g2d);
        draw6(g2d);
        draw7(g2d);
    }

    private void digit4(Graphics2D g2d) {
        draw2(g2d);
        draw3(g2d);
        draw4(g2d);
        draw6(g2d);
    }

    private void digit5(Graphics2D g2d) {
        draw1(g2d);
        draw2(g2d);
        draw4(g2d);
        draw6(g2d);
        draw7(g2d);
    }

    private void digit6(Graphics2D g2d) {
        draw1(g2d);
        draw2(g2d);
        draw4(g2d);
        draw5(g2d);
        draw6(g2d);
        draw7(g2d);
    }

    private void digit7(Graphics2D g2d) {
        draw1(g2d);
        draw3(g2d);
        draw6(g2d);
    }

    private void digit8(Graphics2D g2d) {
        draw1(g2d);
        draw2(g2d);
        draw3(g2d);
        draw4(g2d);
        draw5(g2d);
        draw6(g2d);
        draw7(g2d);
    }

    private void digit9(Graphics2D g2d) {
        draw1(g2d);
        draw2(g2d);
        draw3(g2d);
        draw4(g2d);
        draw6(g2d);
        draw7(g2d);
    }

    private void digit0(Graphics2D g2d) {
        draw1(g2d);
        draw2(g2d);
        draw3(g2d);
        draw5(g2d);
        draw6(g2d);
        draw7(g2d);
    }

    //--------------------------------------------

    private void draw1(Graphics2D g2d) {
        g2d.fillOval(5, 5, 5, 1);
    }

    private void draw2(Graphics2D g2d) {
        g2d.fillOval(5, 5, 1, 5);
    }

    private void draw3(Graphics2D g2d) {
        g2d.fillOval(10, 5, 1, 5);
    }

    private void draw4(Graphics2D g2d) {
        g2d.fillOval(5, 10, 5, 1);
    }

    private void draw5(Graphics2D g2d) {
        g2d.fillOval(5, 10, 1, 5);
    }

    private void draw6(Graphics2D g2d) {
        g2d.fillOval(10, 10, 1, 5);
    }

    private void draw7(Graphics2D g2d) {
        g2d.fillOval(5, 15, 5, 1);
    }

    private void digit(int a, Graphics2D g2d) {
        switch(a) {
            case 0:
                digit0(g2d);
                break;
            case 1:
                digit1(g2d);
                break;
            case 2:
                digit2(g2d);
                break;
            case 3:
                digit3(g2d);
                break;
            case 4:
                digit4(g2d);
                break;
            case 5:
                digit5(g2d);
                break;
            case 6:
                digit6(g2d);
                break;
            case 7:
                digit7(g2d);
                break;
            case 8:
                digit8(g2d);
                break;
            case 9:
                digit9(g2d);
                break;
        }        
    }
}
